//
//  YJXYVC.m
//  Tunnel
//
//  Created by wbb on 2025/6/14.
//

#import "YJXYVC.h"
#import "YJXYCell.h"
#import "YJXYDetailVC.h"
#import "YJXYModel.h"
#import "CascadePickerView.h"
#import "MJRefresh/MJRefresh.h"

@interface YJXYVC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) NSMutableArray * dataArr;
@property (nonatomic, strong) UITableView * tableView;
@property (nonatomic, strong) UIButton * btn1;
@property (nonatomic, strong) UIButton * btn2;
@property (nonatomic, strong) UIButton * btn3;
@property (nonatomic,copy) NSString*  direction;
@property (nonatomic) NSInteger  page;
@property (nonatomic,copy) NSArray*  filterArr;
@property (nonatomic,copy) NSArray*  keyArr;
@property (nonatomic,copy) NSString*  sjdj;
@property (nonatomic,copy) NSString*  sjlx;
@property (nonatomic,copy) NSArray*  sjztList;

@end

@implementation YJXYVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"应急事件";
    self.dataArr = [NSMutableArray array];
    self.keyArr = @[@"contingencyevent_status",@"contingencyevent_type",@"contingencyevent_level"];
    [self initUI];
    kWEAK_SELF;
    [TYNetWorkTool postRequest:TYGetDictQuery parameters:@{
        @"types":weakSelf.keyArr
    } successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
        if(success){
            NSMutableArray * array = [NSMutableArray array];
            for (NSString * key in weakSelf.keyArr) {
                NSMutableArray *arr = [NSMutableArray array];
                if([key isEqualToString:@"contingencyevent_status"]){
                    [arr addObject:@{@"dictLabel":@"事件状态",@"dictValue":@""}];
                }
                if([key isEqualToString:@"contingencyevent_type"]){
                    [arr addObject:@{@"dictLabel":@"事件类型",@"dictValue":@""}];
                }
                if([key isEqualToString:@"contingencyevent_level"]){
                    [arr addObject:@{@"dictLabel":@"事件等级",@"dictValue":@""}];
                }
                for (NSDictionary * dict in  data[@"data"]) {
                    if([dict[@"dictType"] isEqualToString:key]){
                        [arr addObject:dict];
                    }
                }
                [array addObject:arr];
            }
            weakSelf.filterArr = array.copy;
            
        }
    } failureBlock:^(NSString * _Nonnull description) {
                
    }];
    
}
- (void)initUI {
    UIView *topView = [[UIView alloc] init];
    [self.view addSubview:topView];
    [topView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.top.equalTo(self.view.mas_safeAreaLayoutGuideTop);
        make.height.offset(50);
    }];
    
    
    UIButton *btn1 = [UIButton buttonWithTitle:@"事件状态" titleColor:main_light_text_color_6 font:[UIFont systemFontOfSize:14] imageName:@"select_icon"  target:self action:@selector(onType:)];
    btn1.tag = 10;
    [topView addSubview:btn1];
    self.btn1 = btn1;

    UIButton *btn2 = [UIButton buttonWithTitle:@"事件类型" titleColor:main_light_text_color_6 font:[UIFont systemFontOfSize:14] imageName:@"select_icon"  target:self action:@selector(onType:)];
    btn2.tag = 11;
    [topView addSubview:btn2];
    self.btn2 = btn2;

    
    UIButton *btn3 = [UIButton buttonWithTitle:@"事件等级" titleColor:main_light_text_color_6 font:[UIFont systemFontOfSize:14] imageName:@"select_icon"  target:self action:@selector(onType:)];
    btn3.tag = 12;
    [topView addSubview:btn3];
    self.btn3 = btn3;
    
    [btn1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.bottom.offset(0);
        
    }];
    
    [btn3 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.bottom.offset(0);
        
    }];
    
    [btn2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.bottom.offset(0);
        make.left.equalTo(btn1.mas_right);
        make.right.equalTo(btn3.mas_left);
        make.width.equalTo(btn1.mas_width);
        make.width.equalTo(btn3.mas_width);

    }];
    
    [btn1 layoutButtonWithEdgeInsetsStyle:TYButtonEdgeInsetsStyleRight imageTitleSpace:5];
    [btn2 layoutButtonWithEdgeInsetsStyle:TYButtonEdgeInsetsStyleRight imageTitleSpace:5];
    [btn3 layoutButtonWithEdgeInsetsStyle:TYButtonEdgeInsetsStyleRight imageTitleSpace:5];
    
    
    
    [self.view addSubview:self.tableView];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(topView.mas_bottom);
        make.left.right.offset(0);
        make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
    }];
    kWEAK_SELF
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.page = 1;
        [weakSelf initRequest];
    }];
    MJRefreshAutoNormalFooter *footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.page ++;
        [weakSelf initRequest];
    }];
    [footer setTitle:@"没有更多了" forState:MJRefreshStateNoMoreData];
    self.tableView.mj_footer = footer;
    [self.tableView.mj_header beginRefreshing];
}
-(void)initRequest{
    kWEAK_SELF;
    [TYNetWorkTool postRequest:TYGetEmergencyEvent parameters:@{
        @"sjdj":weakSelf.sjdj?:@"",
        @"sjlx":weakSelf.sjlx?:@"",
        @"sjztList":weakSelf.sjztList?:@[],
        @"pageNum": @(weakSelf.page),
        @"pageSize": @20
      } successBlock:^(BOOL success, id  _Nonnull data, NSString * _Nonnull msg) {
        // 进入DispatchGroup，表示开始一个任务
        NSLog(@"-------1111-----------");
        if(success) {
            if(self.page == 1){
                [weakSelf.dataArr removeAllObjects];
            }
            
           [weakSelf.dataArr addObjectsFromArray:[NSArray yy_modelArrayWithClass:YJXYModel.class json:data[@"data"][@"list"]]];
            [weakSelf.tableView reloadData];
            weakSelf.hasNextPage =  [data[@"data"][@"hasNextPage"] boolValue];

        }
        [weakSelf.tableView.mj_header endRefreshing];
        if (!weakSelf.hasNextPage) {
            [weakSelf.tableView.mj_footer endRefreshingWithNoMoreData];
        }else{
            [weakSelf.tableView.mj_footer endRefreshing];
        }
    } failureBlock:^(NSString * _Nonnull description) {
        // 进入DispatchGroup，表示开始一个任务
        
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
}
- (void)onType:(UIButton *)btn {
    NSInteger tag = btn.tag-10;
    NSArray * array = self.filterArr[tag];
    NSMutableArray * leftData = [NSMutableArray array];
    for (NSDictionary * dict in array) {
        [leftData addObject:dict[@"dictLabel"]];
    }
    kWEAK_SELF;
    // 创建并显示 picker
    CascadePickerView* pickerView = [[CascadePickerView alloc] initWithLeftData:leftData columns:1];
    pickerView.completionIndexBlock = ^(NSInteger leftIndex, NSInteger rightIndex) {
        NSDictionary * dict =  array[leftIndex];
        if(tag == 0){
            [weakSelf.btn1 setTitle:dict[@"dictLabel"] forState:UIControlStateNormal];
            weakSelf.sjztList = [dict[@"dictValue"] isEqualToString:@""]?@[]:@[dict[@"dictValue"]];

        }else if (tag == 1){
            [weakSelf.btn2 setTitle:dict[@"dictLabel"] forState:UIControlStateNormal];
            weakSelf.sjlx = dict[@"dictValue"];
        }else if (tag == 2){
            [weakSelf.btn3 setTitle:dict[@"dictLabel"] forState:UIControlStateNormal];
            weakSelf.sjdj = dict[@"dictValue"];
        }
        [weakSelf.tableView.mj_header beginRefreshing];
    };
    [pickerView show];
}

#pragma mark ---- UITableViewDelegate, UITableViewDataSource ----
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.dataArr.count;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    return 150;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YJXYCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (!cell) {
        cell = [[YJXYCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
    }
    cell.typeArr = self.filterArr[1];
    if (indexPath.row < self.dataArr.count) {
        cell.model =  self.dataArr[indexPath.row];
    }
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    YJXYModel*model = self.dataArr[indexPath.row];
    YJXYDetailVC *vc = [[YJXYDetailVC alloc] init];
    vc.ID = model.ID;
    [self.navigationController pushViewController:vc animated:YES];

    
}
#pragma mark ---- lazy ----
- (UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.backgroundColor =[UIColor clearColor];
                _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _tableView;
}
@end
